package com.goroom.v_timer;

import java.text.SimpleDateFormat;
import java.util.*;

public class TimerTemplate {
    public static void main(String[] args){
        TimerTask timerTask1 = new MyTimerTask();
        TimerTask timerTask2 = new MyTimerTask();
        TimerTask timerTask3 = new MyTimerTask();
        TimerTask timerTask4 = new MyTimerTask();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(sdf.format(date));

        //启动之后，延时1s执行
        new Timer().schedule(timerTask1, 1000);
        //启动之后，延时ss开始执行，在首次执行之后每隔2s执行一次
        new Timer().schedule(timerTask2, 2000, 2000);

        new Timer().schedule( timerTask3 ,date );
        new Timer().schedule( timerTask4 ,date , 2000);
    }

    static class MyTimerTask extends TimerTask{
        @Override
        public void run() {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            System.out.println("["+Thread.currentThread().getName() +"] " + sdf.format(date));
        }
    }


}
